/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplerecipes.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 *
 * @author matamh0a
 */
public class MesuredIngredient {
    
    double quantity;
    Unit unit;
    Ingredient ingredient;
    String details;
    double percentage;
    double resizeFactor = 1;
    
    protected double weight;
    protected double calories;
    protected double cost;
    protected boolean included = true;
    
    public static final String PROP_INCLUDED = "included";
    public static final String PROP_COST = "cost";
    public static final String PROP_WEIGHT = "weight";
    public static final String PROP_CALORIES = "calories";
    
    boolean updatePercentage = true;
    
    public MesuredIngredient(Ingredient ingredient) {
        this.ingredient = ingredient;
        unit = Unit.g;
        details = "";
    }


    public Ingredient getIngredient() {
        return ingredient;
    }

    public void setIngredient(Ingredient ingredient) {
        this.ingredient = ingredient;
        //Quantity --> Weight, Calories
        setWeight(quantity * ingredient.getWeight(unit));
        setCalories(quantity * ingredient.getCalories(unit));
        setCost(quantity * ingredient.getCost(unit));
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public double getPercentage() {
        return percentage;
    }

    public void setPercentage(double percentage) {
        double oldPercentage = this.percentage;
        this.percentage = percentage;
//        System.out.println(oldPercentage  + " --> " + quantity + unit);
//        System.out.println(percentage  + " --> " + (percentage * quantity/ oldPercentage) + unit);
        setQuantity(percentage * quantity/ oldPercentage);
    }
    
    public void updatePercentage(double tw) {
        if(!isUpdatePercentage())
            return;
//        System.out.println("update%:" + weight + " / " + tw);
        percentage = weight / tw;
    }
    
    public double getQuantity() {
        return quantity;
    }

    public void setQuantity(double quantity) {
        quantity = quantity * resizeFactor;
        this.quantity = quantity;
        //Quantity --> Weight, Calories, Cost
        setWeight(quantity * ingredient.getWeight(unit));
        setCalories(quantity * ingredient.getCalories(unit));
        setCost(quantity * ingredient.getCost(unit));
    }

    public Unit getUnit() {
        return unit;
    }

    public void setUnit(Unit unit) {
        this.unit = unit;
        //Unit --> Weight, Calories
        setWeight(quantity * ingredient.getWeight(unit));
        setCalories(quantity * ingredient.getCalories(unit));
        setCost(quantity * ingredient.getCost(unit));
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        double oldWeight = this.weight;
        this.weight = weight;
        propertyChangeSupport.firePropertyChange(PROP_WEIGHT, oldWeight, weight);
    }
    
    public double getCalories() {
        return calories;
    }

    public void setCalories(double calories) {
        double oldCalories = this.calories;
        this.calories = calories;
        propertyChangeSupport.firePropertyChange(PROP_CALORIES, oldCalories, calories);
    }
    
    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        System.out.println("setCost(" + cost  +")");
        double oldCost = this.cost;
        this.cost = cost;
        propertyChangeSupport.firePropertyChange(PROP_COST, oldCost, cost);
    }
   
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    public boolean isUpdatePercentage() {
        return updatePercentage;
    }

    public void setUpdatePercentage(boolean updatePercentage) {
        this.updatePercentage = updatePercentage;
    }

    public double getResizeFactor() {
        return resizeFactor;
    }

    public void setResizeFactor(double resizeFactor) {
        double rf = this.resizeFactor;
        this.resizeFactor = resizeFactor;
        //update Quantity
        setQuantity(quantity * 1/rf);
    }
    
    public boolean isIncluded() {
        return included;
    }

    public void setIncluded(boolean included) {
        boolean oldIncluded = this.included;
        this.included = included;
        propertyChangeSupport.firePropertyChange(PROP_INCLUDED, oldIncluded, included);
    }

}
